﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LTM_Extra;
using System.IO;
using Microsoft.Office.Tools.Ribbon;

namespace LTM_Main
{
    public partial class CreationDocument : Form
    {
        RibbonGallery styleGallery = null;
        public CreationDocument(RibbonGallery styleGallery)
        {
            InitializeComponent();
            this.styleGallery = styleGallery;
            

        }
        ContentProvider contentProvider = new ContentProvider();
      

        private void CreationDocument_Load(object sender, EventArgs e)
        {try
            {
            var authorList = contentProvider.GetAuthorList();
            var defaultAuthor = contentProvider.GetAppSettings(Constants.DefaultAuthorKey);

            cmbSelectAuthor.DataSource = authorList;
            cmbSelectAuthor.SelectedIndex = authorList.IndexOf(defaultAuthor);



            lstCategory.DataSource = contentProvider.GetWordTemplateCategories();
             }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }
        private void SetupStylesMenuItem()
        {

           //var styles = new List<Style>();
            styleGallery.Items.Clear();
            foreach (var style in Helper.GetStylesFromTemplate(contentProvider))
            {
                RibbonDropDownItem item=new RibbonDropDownItem();
                item.Label=style.Value.FriendlyName;
                item.ScreenTip=style.Value.Discription;
                item.Tag=style.Value.Name;

                styleGallery.Items.Add(item);

               
            }
            if (styleGallery.Items.Count > 0)
            {
                styleGallery.Enabled = true;
            }
            //int groupsLength = styles.Count / btnGroups.Count;
            //int groupsModel = styles.Count % btnGroups.Count;
            //for (int n = 0; n < groupsLength; n++)
            //{
            //    for (int i = 0; i < btnGroups.Count; i++)
            //    {
            //        var btnGroup = btnGroups[i];
            //        var btn = new RibbonButton();
            //        var style = styles[n * btnGroups.Count + i];
            //        btn.Label = style.Name;
            //        btnGroup.Items.Add(btn);

            //    }

            //}

            //for (int i = 0; i < groupsModel; i++)
            //{
            //    var btnGroup = btnGroups[i];
            //    var btn = new RibbonButton();
            //    var style = styles[i];
            //    btn.Label = style.Name;
            //}

        }


        private void btnOK_Click(object sender, EventArgs e)
        {

            try
            {

                string templatePath = Path.Combine(contentProvider.GetAppSettings(Constants.TemplateFolderKey), @"Word\" + ((TableOfContentsWordCategoriesTemplates)(lstTemplate.SelectedValue)).fileName);

                if (!File.Exists(templatePath))
                {
                    MessageBox.Show("Can't find template: " + templatePath);
                }
                else
                {
                    Helper.ApplyVariablesToTemplate(templatePath, GetAllVariblesValue());
                    SetupStylesMenuItem();
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            Close();

        }

       

        private IDictionary<string, string> GetAllVariblesValue()
        {
            IDictionary<string, string> keyValuePairs = new Dictionary<string, string>();

            foreach (var item in Helper.GetPropertiesFromTableLayout(tlpVariables))
            {
                keyValuePairs.Add(item);

            }



            foreach (var item in contentProvider.GetAuthorProperties(cmbSelectAuthor.SelectedValue.ToString()))
            {
                keyValuePairs.Add(item);
            }

            return keyValuePairs;
        }

      

        private void lstTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {try
            {
            Helper.InitTableLayoutPanel(tlpVariables, contentProvider.GetVariablesFormSelectedTemplate(lstCategory.SelectedValue.ToString(), ((TableOfContentsWordCategoriesTemplates)(lstTemplate.SelectedValue)).fileName));
             }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void lstCategory_SelectedIndexChanged(object sender, EventArgs e)
        {try
            {
            lstTemplate.DataSource = contentProvider.GetWordTemplatesByCategory(lstCategory.SelectedValue.ToString());
            lstTemplate.DisplayMember = "fileName";
             }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
